#include "ClientUI.h"

#include<QMessageBox>
#include<QByteArray>

ClientUI::ClientUI(QWidget *parent)
    : QWidget(parent){
    setWindowTitle(tr("TCP Client"));

    ListWidgetnews = new QListWidget;
    LineEditmsg = new QLineEdit;
    LineEditname = new QLineEdit(tr("Jack"));
    LineEditserverIp = new QLineEdit;
    LineEditport = new QLineEdit(tr("8888"));
    LabelName = new QLabel(tr("nick name:"));
    LabelServerIP = new QLabel(tr("server IP:"));
    LabelPort = new QLabel(tr("port     :"));
    BtnsendMsg = new QPushButton(tr("send"));
    Btnswitch = new QPushButton(tr("enter"));
    Btnhistory = new QPushButton(tr("history"));

    historyUI = new HistoryUI(this);

    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(ListWidgetnews,0,0,4,2);
    mainLayout->addWidget(LineEditmsg,4,0);
    mainLayout->addWidget(BtnsendMsg,4,1);
    mainLayout->addWidget(LabelName,5,0);
    mainLayout->addWidget(LineEditname,5,1);
    mainLayout->addWidget(LabelServerIP,6,0);
    mainLayout->addWidget(LineEditserverIp,6,1);
    mainLayout->addWidget(LabelPort,7,0);
    mainLayout->addWidget(LineEditport,7,1);
    mainLayout->addWidget(Btnswitch,8,0,1,2);
    mainLayout->addWidget(Btnhistory,9,0,1,2);


    isConnected = false;
    port = 8888;
    clientSocket = new ClientSocket(this);
    if(!clientSocket->bind(port)){
         QMessageBox::information(this,tr("Error"),tr("Failed to create a Tcp Socket!"));
     }

    connect(this,SIGNAL(sendMsg(QString)),clientSocket,SLOT(slotsendMsg(QString)));
    connect(this,SIGNAL(connectServer(QString,QString)),
            clientSocket,SLOT(slotconnectServer(QString,QString)));
    connect(this,SIGNAL(disconnectServer()),clientSocket,SLOT(slotdisconnectServer()));

    connect(clientSocket,SIGNAL(updateClient(QString)),this,SLOT(slotupdateClient(QString)));
    connect(clientSocket,SIGNAL(connected()),this,SLOT(slotConnected()));
    connect(clientSocket,SIGNAL(disconnected()),this,SLOT(slotdisconnected()));
    connect(clientSocket,SIGNAL(updateHistory(QString)),this,SLOT(slotupdateHistory(QString)));

    connect(this,SIGNAL(updateHistoryUI(QString)),historyUI,SLOT(updateHistoryUI(QString)));

    connect(BtnsendMsg,SIGNAL(clicked(bool)),this,SLOT(slotSendMsg()));
    connect(Btnswitch,SIGNAL(clicked(bool)),this,SLOT(slotSwitch()));
    connect(Btnhistory,SIGNAL(clicked(bool)),this,SLOT(slotShowhistory()));
}

ClientUI::~ClientUI(){
}

void ClientUI::slotSendMsg(){
    QString name = LineEditname->text();
    QString msg = LineEditmsg->text();
    if(!msg.isEmpty()){
        msg = name + ":  " + msg;
        emit sendMsg(msg);
    }
}

void ClientUI::slotSwitch(){
    if(LineEditserverIp->text().isEmpty()){
        QMessageBox::information(this,tr("Error"),tr("please input the server IP"));
    }
    else{
      if(!isConnected){
          QString name = LineEditname->text();
          QString serverIP = LineEditserverIp->text().trimmed();
          QString Port = LineEditport->text();

          emit connectServer(serverIP,Port);
      }
      else{
          emit disconnectServer();
      }
    }
}

void ClientUI::slotConnected(){
    Btnswitch->setText(tr("leave"));
    isConnected = true;
}

void ClientUI::slotdisconnected(){
    Btnswitch->setText(tr("enter"));
    isConnected = false;
}

void ClientUI::slotupdateClient(QString str){
    ListWidgetnews->addItem(str);
}

void ClientUI::slotShowhistory(){
    historyUI->show();
}

void ClientUI::slotupdateHistory(QString str){
    emit updateHistoryUI(str);
}
